package itsm.isperp.module.controller.individuation; 

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.individuation.IddProcessRelation;
import itsm.isperp.module.service.individuation.IddProcessRelationService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2017-11-15 16:45
 */
@Controller
@RequestMapping("individuation/processRelation")
public class IddProcessRelationController extends BaseController<IddProcessRelation> {

	@Autowired
	protected IddProcessRelationService iddProcessRelationService;

	@Override
	public IddProcessRelationService getService() {
		return this.iddProcessRelationService;
	}
	
	@Override
	public String getPageTitle() {
		return "自定义关联流程定义";
	}
	
	@RequestMapping("getOutlineInfoAndCantRelateProcess")
  @ResponseBody
  public Map<String,Object> getOutlineInfoAndCantRelateProcess(String defId,String nodeId) {
    return this.getService().getOutlineInfoAndCantRelateProcess(defId,nodeId);
  }
	
	@RequestMapping("addOrDelete")
  @ResponseBody
  public void addOrDelete(HttpServletRequest req) {
	  String outlineId=req.getParameter("outlineId");
	  String relateProcess=req.getParameter("relateProcess");
	  String nodeId=req.getParameter("nodeId");
	  String opType=req.getParameter("opType");
	  String prokey=req.getParameter("prokey");
	  String sql;
	  if(opType.equals("false"))
	  {
	    sql="delete from idd_process_relation where outline_id='"+outlineId+"' and node_id='"+nodeId+"' and relate_process_keys='"+relateProcess+"'";
	  }else
	  {
	    sql="insert into idd_process_relation(id,outline_id,node_id,relate_process_keys,prokey)" +
	    		" values(uuid(),'"+outlineId+"','"+nodeId+"','"+relateProcess+"','"+prokey+"')";
      
	  }
	  JdbcTemplate jdbcTemplate=ContextHolder.getSpringBean("jdbcTemplate");
	  jdbcTemplate.update(sql);
    
  }
	
	@RequestMapping("getInfo")
  @ResponseBody
  public List<Map<String,Object>> getInfo(HttpServletRequest req) {
    String nodeId=req.getParameter("nodeId");
    String sql="select * from idd_process_relation where node_id='"+nodeId+"'";
    JdbcTemplate jdbcTemplate=ContextHolder.getSpringBean("jdbcTemplate");
    List<Map<String,Object>> li=jdbcTemplate.queryForList(sql);
    return li;
  }
	
	@RequestMapping("getRunInfo")
  @ResponseBody
  public ResultMessage getRunInfo(HttpServletRequest req) {
    String nextOutLineId=req.getParameter("nextOutLineId");
    String curNodeId=req.getParameter("curNodeId");
    String key=req.getParameter("key");
    boolean returnResult=true;
    String unTakeUpProcess="";
    String unFinishProcess="";
    String sql="SELECT * FROM idd_process_relation  WHERE  (outline_id='"+nextOutLineId+"' and node_id='"+curNodeId+"')";
    JdbcTemplate jdbcTemplate=ContextHolder.getSpringBean("jdbcTemplate");
    List<Map<String,Object>> li=jdbcTemplate.queryForList(sql);
    if(li.size()==0)
    {
      return ResultMessage.newInstance(true,"");
    }else
    {
      for(int i=0;i<li.size();i++)
      {
         String relateProcessKey=li.get(i).get("relate_process_keys").toString();
         
         Integer result=this.getService().getDealStatus(key, relateProcessKey);
         if(result==2)
         {
           String name=this.getService().getProcessNameByKey(relateProcessKey);
           unTakeUpProcess+=name+",";
           returnResult=false;
         }else if(result==3)
         {
           String name=this.getService().getProcessNameByKey(relateProcessKey);
           unFinishProcess+=name+",";
           returnResult=false;
         }
      }
    }
    
    if(returnResult)
    {
      return ResultMessage.newInstance(true,"");
    }else
    {
      if(StringUtils.isNoneBlank(unTakeUpProcess))
      {
        unTakeUpProcess=unTakeUpProcess.substring(0,unTakeUpProcess.length()-1);
        return ResultMessage.newInstance(false,"请发起关联流程【"+unTakeUpProcess+"】");
      }else if(StringUtils.isNoneBlank(unFinishProcess))
      {
        unFinishProcess=unFinishProcess.substring(0,unFinishProcess.length()-1);
        return ResultMessage.newInstance(false,"关联流程【"+unFinishProcess+"】未完成");
      }else
      {
        return ResultMessage.newInstance(true,"");
      }
    }
    
  }

	
	
}
